<template>
  <div class="table-box card">
    <div>
      <el-alert title="温馨提示：该设置只适用于副屏收银机" show-icon type="info" :closable="false" />
    </div>
    <div class="mt20">
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="168px" status-icon>
        <el-row :gutter="100">
          <el-col :span="10">
            <div>
              <el-text class="fsize18">
                副屏预览
                <el-tooltip effect="dark" content="预览效果仅供参考，轮播效果请在收银机副屏查看" placement="top">
                  <el-icon><QuestionFilled /></el-icon>
                </el-tooltip>
              </el-text>
              <p class="text-describe-color">未点餐时效果</p>
              <div class="group-img">
                <img class="w100" :src="ruleForm.unOrderImagesObj.length > 0 ? ruleForm.unOrderImagesObj[0].url : normalBanner" />
              </div>
              <p class="text-describe-color">点餐时效果</p>
              <div class="group-img">
                <img
                  class="w100"
                  :src="
                    ruleForm.orderSetType === 3
                      ? notShowGoodsPrice
                      : ruleForm.orderSetType === 2
                      ? notShowGoodsPrice
                      : normalBanner
                  "
                />
              </div>
            </div>
          </el-col>
          <el-col :span="14">
            <div>
              <el-text class="fsize18">未点餐时设置</el-text>
              <el-form-item label="未点餐时展示 :">
                <span>轮播广告图</span>
              </el-form-item>
              <el-form-item label="轮播图切换时间间隔 :" prop="unOrderInterval">
                <el-radio-group v-model="ruleForm.unOrderInterval">
                  <el-radio :label="1">3s</el-radio>
                  <el-radio :label="2">5s</el-radio>
                  <el-radio :label="3">15s</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="轮播图上传 :" prop="unOrderImagesObj">
                <div>
                  <div class="text-describe-color">
                    建议图片比例为16:9, 大小5MB以内,最多可上传20张, 图片格式仅支持bmp, jpeg, jpg, png, gif
                  </div>
                  <UploadImgs v-model:file-list="ruleForm.unOrderImagesObj" width="102px" height="102px">
                    <template #empty>
                      <el-icon><Picture /></el-icon>
                      <span>上传</span>
                    </template>
                  </UploadImgs>
                </div>
              </el-form-item>
            </div>
            <div class="mt50">
              <el-text class="fsize18">点餐时设置</el-text>
              <el-form-item label="点餐时展示 :" prop="orderSetType">
                <div>
                  <el-radio-group v-model="ruleForm.orderSetType">
                    <el-radio :label="1">轮播广告图</el-radio>
                    <el-radio :label="2">点餐内容</el-radio>
                    <!-- <el-radio :label="3">点餐内容+所点菜品图片</el-radio> -->
                  </el-radio-group>
                  <div class="text-describe-color">点餐内容包含菜品名称、数量、总价、应付等信息</div>
                </div>
              </el-form-item>
              <el-form-item label="轮播图切换时间间隔 :" prop="orderInterval">
                <el-radio-group v-model="ruleForm.orderInterval">
                  <el-radio :label="1">3s</el-radio>
                  <el-radio :label="2">5s</el-radio>
                  <el-radio :label="3">15s</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="轮播图上传 :" prop="orderImagesObj">
                <div>
                  <div class="text-describe-color">
                    建议图片比例为1:1, 大小5MB以内, 最多可上传20张, 图片格式仅支持bmp,jpeg, jpg, png, gif
                  </div>
                  <UploadImgs v-model:file-list="ruleForm.orderImagesObj" width="102px" height="102px">
                    <template #empty>
                      <el-icon><Picture /></el-icon>
                      <span>上传</span>
                    </template>
                  </UploadImgs>
                </div>
              </el-form-item>
              <el-button type="primary" @click="onSubmit">保存设置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>
<script setup lang="tsx" name="screen">
import { reactive, ref, onMounted } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { editScreen, getScreenInfo } from "@/api/modules/restaurant";
import normalBanner from "@/assets/images/normal-banner.png";
import notShowGoodsPrice from "@/assets/images/notshow-goodsprice.png";
const ruleFormRef = ref<FormInstance>();

interface RuleForm {
  unOrderInterval: number;
  orderInterval: number;
  orderSetType: number;
  unOrderImagesObj: string[];
  orderImagesObj: string[];
}
const ruleForm = ref<RuleForm>({
  unOrderInterval: 1,
  orderInterval: 1,
  orderSetType: 2,
  unOrderImagesObj: [],
  orderImagesObj: []
});

const rules = reactive<FormRules<RuleForm>>({
  // unOrderImagesObj: [{ required: true, message: "请上传图片", trigger: "blur" }]
});

const onSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await editScreen(ruleForm.value).then(res => {
        ElMessage({
          type: "success",
          message: res.msg
        });
      });
    } catch (error) {
      console.log(error);
    }
  });
};

onMounted(async () => {
  try {
    const [screenInfo] = await Promise.all([getScreenInfo()]);
    ruleForm.value = screenInfo.data;
  } catch (error) {
    // 错误处理逻辑
    console.error("An error occurred:", error);
  }
});
</script>
<style lang="scss" scoped>
.group-img {
  max-width: 400px;
}
</style>
